//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

#define THREADAMOUNT        100

uint_t          g_uPriority;
IThread         *g_pIPriorityThread[THREADAMOUNT];

Int32 SetMyselfPriority(void *pvPara)
{
    while (1) {
        DzYield();
    }

    return NOERROR;
}

EXTERN void ThreadPrioritytst()
{
    ECode       ec;
    uint_t      i;

    g_uPriority = 1;

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = DzCreateKernelThread(SetMyselfPriority, (void *)i, 0,
                                (ThreadPriorityRank)0xFFFFEEEE, &g_pIPriorityThread[i]);
        if (FAILED(ec)) {
            kprintf("Create thread %d failed, the ec = 0x%08x\n", i, ec);
            assert(0);

            return;
        }
    }

    for (i = 0; i < 2000; i++) {
        for (int j = 0; j < THREADAMOUNT; j++) {
            ec = g_pIPriorityThread[j]->
                            SetPriority((ThreadPriority)(g_uPriority % 5 + 1));
            assert(NOERROR == ec);

            ThreadPriority  thdPriority;
            ec = g_pIPriorityThread[j]->GetPriority(&thdPriority);
            assert((ThreadPriority)(g_uPriority % 5 + 1) == thdPriority);

            g_uPriority++;
        }
    }

    for (int j = 0; j < THREADAMOUNT; j++) {
        ec = g_pIPriorityThread[j]->GetPriority(NULL);
        assert(E_INVALID_ARGUMENT == ec);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        ec = g_pIPriorityThread[i]->Abort();
        assert(NOERROR == ec);
    }

    for (i = 0; i < THREADAMOUNT; i++) {
        g_pIPriorityThread[i]->Release();
    }
}
